home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / misc / Radio / radio / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-14  |  3.2 KB  |  190 lines

  1. /*
  2.  * Read "n" bytes from a descriptor.
  3.  * Use in place of read() when fd is a stream socket
  4.  *
  5.  * Returns the number of total bytes read.
  6.  */
  7.  
  8. int
  9. readn(fd, ptr, nbytes)
  10. int fd;
  11. char *ptr;
  12. int nbytes;
  13. {
  14.     int nleft, nread;
  15.  
  16.     nleft = nbytes;
  17.     while (nleft > 0) {
  18.         nread = read(fd, ptr, nleft);
  19.         if (nread < 0)
  20.             return(nread);    /* error, return <0 */
  21.         else if (nread == 0)    /* EOF */
  22.             break;
  23.     
  24.         nleft     -= nread;
  25.         ptr     += nread;
  26.     }
  27.     return(nbytes - nleft);    /* return >= 0) */
  28. }
  29.  
  30.  
  31.  
  32. /*
  33.  * Write "n" bytes to a descriptor.
  34.  * Use in place of write() when fd is a stream socket
  35.  *
  36.  * We return the number of bytes written
  37.  */
  38.  
  39. int
  40. writen(fd, ptr, nbytes)
  41. int    fd;
  42. char    *ptr;
  43. int    nbytes;
  44. {
  45.     int nleft, nwritten;
  46.  
  47.     nleft = nbytes;
  48.     while(nleft > 0) {
  49.         nwritten = write(fd, ptr, nleft);
  50.         if (nwritten <= 0)
  51.             return(nwritten);    /* error */
  52.  
  53.         nleft    -= nwritten;
  54.         ptr    += nwritten;
  55.     }
  56.     return(nbytes - nleft);
  57. }
  58.  
  59.  
  60. /*
  61.  * Writestring uses the writen and strlen calls to write a
  62.  * string to the file descriptor fd.  If the write fails
  63.  * a -1 is returned. Otherwise zero is returned.
  64.  */
  65.  
  66. int
  67. writestring(fd, stringptr)
  68. int    fd;
  69. char    *stringptr;
  70. {
  71.     int length;
  72.  
  73.     length = strlen(stringptr);
  74.     if (writen(fd, stringptr, length) != length)
  75.         return(-1);
  76.     else
  77.         return(0);
  78. }
  79.  
  80.  
  81. /*
  82.  * Read a line from a descriptor.  Read the line one byte at a time,
  83.  * looking for the newline.  We store the newline in the buffer,
  84.  * then follow it with a null (the same as fgets(3)).
  85.  * We return the number of characters up to, but not including,
  86.  * the null (the same as strlen(3))
  87.  */
  88.  
  89. int
  90. readline(fd, ptr, maxlen)
  91. int    fd;
  92. char    *ptr;
  93. int     maxlen;
  94. {
  95.     int n;
  96.     int rc;
  97.     char c;
  98.  
  99.     for (n=1; n < maxlen; n++) {
  100.         if ( (rc = read(fd, &c, 1)) == 1) {
  101.             *ptr++ = c;
  102.             if (c == '\n')
  103.                 break;
  104.         }
  105.         else if (rc == 0) {
  106.             if (n == 1)
  107.                 return(0);    /* EOF, no data read */
  108.             else
  109.                 break;        /* EOF, some data was read */
  110.         }
  111.         else
  112.             return(-1);        /* error */
  113.     }
  114.  
  115.     *ptr = 0;                 /* Tack a NULL on the end */
  116.     return(n);
  117. }
  118.  
  119.  
  120. /*
  121.  * Read a stream socket one line at a time, and write each
  122.  * line back to the sender
  123.  *
  124.  * Return when the connection is terminated
  125.  */
  126.  
  127. #define MAXLINE 512
  128.  
  129. void
  130. str_echo(sockfd)
  131. int sockfd;
  132. {
  133.     int n;
  134.     char line[MAXLINE];
  135.  
  136.     for ( ; ; ){
  137.         n = readline(sockfd, line, MAXLINE);
  138.         if (n == 0)
  139.             return;
  140.         else if (n < 0)
  141.             err_dump("str_echo: readline error");
  142.  
  143.         if (writen(sockfd, line, n) != n)
  144.             err_dump("str_echo: writen error");
  145.     }
  146. }
  147.  
  148.  
  149.  
  150. /*
  151.  * Read the contents of the FILE *fp, write each line to the
  152.  * stream socket (to the server process), then read a line back from
  153.  * the socket and write it to the standard output.
  154.  *
  155.  * Return to the caller when an EOF is encountered on the input file.
  156.  */
  157.  
  158. #include <stdio.h>
  159. #define     MAXLINE 512
  160.  
  161. void
  162. str_cli(fp, sockfd)
  163. FILE *fp;
  164. int sockfd;
  165. {
  166.     int n;
  167.     char sendline[MAXLINE];
  168.     char recline[MAXLINE+1];
  169.  
  170.     while (fgets(sendline, MAXLINE, fp) != NULL) {
  171.         n = strlen(sendline);
  172.         if (writen(sockfd, sendline, n) != n)
  173.             err_sys("str_cli: writen error on socket");
  174.     
  175.         /*
  176.          * Now read a line from the socket and write it to
  177.          * our standard output
  178.          */
  179.  
  180.         n = readline(sockfd, recline, MAXLINE);
  181.         if (n<0)
  182.             err_dump("str_cli: readline error");
  183.         recline[n]=0;
  184.         fputs(recline, stdout);
  185.     }
  186.  
  187.     if (ferror(fp))
  188.         err_sys("str_cli: error reading file");
  189. }
  190.